﻿create PROCEDURE Sync.SP_LoanSocialResearchAttachments_SelectChanges
@LastRowVersion rowversion,
@FilterdColumn UNIQUEIDENTIFIER
AS 
BEGIN 
	SELECT 
        t.[LoanSocialResearchAttachmentID],
		g.[LoanSocialResearchID],g.[Attachment],
		t.IsDeleted,
		t.LastUpdatedDate
	FROM [dbo].[LoanSocialResearchAttachments] g RIGHT JOIN Sync.LoanSocialResearchAttachments_Tracking t  
    ON t.LoanSocialResearchAttachmentID = g.LoanSocialResearchAttachmentID 
    join [LoanSocialResearch] sresearch
		on g.LoanSocialResearchID = sresearch.LoanSocialResearchID
		join [CustomerLoans] cl
		on sresearch.LoanID = cl.LoanID
	WHERE (@LastRowVersion IS NULL OR t.Version > @LastRowVersion)
	AND ((@FilterdColumn IS NULL) OR (cl.CommitteeID = @FilterdColumn))
    END














